package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.mapper.TbMenuMapper;
import com.zhentao.mapper.TbRoleMapper;
import com.zhentao.mapper.TbRoleMenuMapper;
import com.zhentao.mapper.TbUserRoleMapper;
import com.zhentao.model.common.dtos.ResponseResult;
import com.zhentao.model.common.enums.AppHttpCodeEnum;
import com.zhentao.model.common.pojo.management.*;
import com.zhentao.service.ITbRoleService;
import com.zhentao.userLocal.AdminUserThreadLocalUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mp
 * @since 2025-01-03
 */
@Service
public class TbRoleServiceImpl extends ServiceImpl<TbRoleMapper, TbRole> implements ITbRoleService {
    @Resource
    private TbRoleMapper roleMapper;
    @Resource
    private TbUserRoleMapper sysUserRoleMapper;
    @Override
    public ResponseResult addRole(TbRole tbRole) {
        //判断参数
        if (tbRole == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //执行查询
        TbRole one = this.getOne(Wrappers.<TbRole>lambdaQuery().eq(TbRole::getRoleName, tbRole.getRoleName()));
        if (one != null) {
            return ResponseResult.errorResult(501,"已经拥有该角色,不可重复添加");
        }
        TbRole role = getRole();
        //判断权限
        if (!role.getRoleName().equals("超级管理员")) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH);
        }
        //执行添加
        int i = roleMapper.insert(tbRole);
        if (i > 0) {
            return ResponseResult.okResult("添加成功");
        }
        return ResponseResult.errorResult(500,"添加失败");
    }

    @Override
    public ResponseResult deleteRole(Integer id) {
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        TbRole role = getRole();
        //判断权限
        if (!role.getRoleName().equals("超级管理员")) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //查询该角色是否被分配
        List<TbUserRole> tbUserRole = sysUserRoleMapper.selectList(Wrappers.<TbUserRole>lambdaQuery().eq(TbUserRole::getRoleId, id));
        if (tbUserRole != null && tbUserRole.size() > 0) {
            return ResponseResult.errorResult(500,"该角色已分配,不可删除");
        }
        //执行删除
        if (roleMapper.deleteById(id) > 0) {
            return ResponseResult.okResult("删除成功");
        }
        return ResponseResult.errorResult(500,"删除失败");
    }

    @Override
    public ResponseResult updateRole(TbRole tbRole) {
        //判断参数
        if (tbRole == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //判断权限
        TbRole role = getRole();
        if (!role.getRoleName().equals("超级管理员")) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //执行修改
        if (roleMapper.updateById(tbRole) > 0) {
            return ResponseResult.okResult("修改成功");
        }
        // TODO 向用户发送站内信
        return  ResponseResult.errorResult(500,"修改失败");
    }
    @Resource
    private TbRoleMenuMapper roleMenuMapper;
    @Resource
    private TbMenuMapper  menuMapper;
    @Override
    public ResponseResult getRoleList(String roleName) {
        TbRole role = getRole();
        if (role.getRoleName().equals("超级管理员") || role.getRoleName().equals("管理员")) {
            List<TbRole> roles = roleMapper.selectList(Wrappers.<TbRole>lambdaQuery().like(StringUtils.checkValNotNull(roleName), TbRole::getRoleName, roleName));
            //遍历角色集合
            roles.forEach(tbRole -> {
                //菜单集合
                List<Integer> menuIds = roleMenuMapper.selectList(Wrappers.<TbRoleMenu>lambdaQuery().eq(TbRoleMenu::getRoleId, tbRole.getId()).select(TbRoleMenu::getMenuId))
                        .stream()
                        .map(TbRoleMenu::getMenuId)
                        .collect(Collectors.toList());
                if (menuIds.size() > 0) {
                    //查询权限名称集合
                    List<String> menuNames = menuMapper.selectList(Wrappers.<TbMenu>lambdaQuery().in(TbMenu::getId, menuIds))
                            .stream()
                            .map(TbMenu::getMenuName)
                            .collect(Collectors.toList());
                    //放入角色集合
                    tbRole.setMenuNames(menuNames);
                }

            });
            return ResponseResult.okResult(roles);
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
    }

    private TbRole getRole() {
        UserInfo sysUser = AdminUserThreadLocalUtils.getUserInfo();
        TbUserRole userRole = sysUserRoleMapper.selectOne(Wrappers.<TbUserRole>lambdaQuery()
                .eq(TbUserRole::getUserId, sysUser.getId()));
        TbRole tbRole = roleMapper.selectOne(Wrappers.<TbRole>lambdaQuery().eq(TbRole::getId, userRole.getRoleId()));

        return tbRole;
    }
}
